<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2020/7/21
  Time: 11:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String basePath = request.getScheme()
            +"://"
            +request.getServerName()
            + ":"
            +request.getServerPort()
            +request.getContextPath()
            +"/";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <title>创建投票项目</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/webjars/jquery/3.5.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            var deleteObj = function() {
                var $trObj = $(this).parent().parent();
                var name = $trObj.find("td:first").text();
                if (confirm("你确定要删除" + name + "吗？")) {
                    $trObj.remove();
                }
                return false;
            }
            $("#addJudge").click(function () {
                var judgename = $("#judgename").val().trim();
                var judgepassword = $("#judgepassword").val().trim();
                var flag=true;
                $("#judgename").val("");
                $("#judgepassword").val("");
                if(judgename===""||judgepassword==="") {
                    $("span.errorMsg").text("评委用户名或密码不能为空！");
                    return false;
                }else {
                    $("input[name='judgename']").each(function(){
                        if($(this).val()===judgename){
                            $("span.errorMsg").text("评委的用户名不能重复！");
                            flag=false;
                            return false;
                        }
                    });
                }
                if(flag===false){
                    return false;
                }
                var $trObj = $("<tr>" + "<td>" +"评委用户名:" + judgename + "</td>" +
                    "<td>"+"评委密码:" + judgepassword + "</td>" +
                    "<input type=\"hidden\" name=\"judgename\" value=" + judgename + ">"  +
                    "<input type=\"hidden\" name=\"judgepassword\" value=" + judgepassword + ">" +
                    "<td><a href=\"deletejudge\">删除</a></td>" +
                    "</tr>");
                $trObj.appendTo($("#judgeTable"));
                $trObj.find("a").click(deleteObj);
                $("span.errorMsg").text("");
            });
            $("#sub_btn").click(function () {
                var information = $("#information").val().trim();
                if(information==="") {
                    $("span.errorMsg").text("项目名不能为空！");
                    return false;
                }
                $("#judgename").remove();
                $("#judgepassword").remove();
            });
            $("a").click(deleteObj);
        });
    </script>
</head>
<body>
<div>
    <form action="projectServlet" method="post">
        <input type="hidden" name="action" value="createProject" />
        <table>
            <tr>
                <td>输入项目名:</td>
                <td>
                    <input type="text" placeholder="请输入项目名" tabindex="1" name="information" id="information" value="${requestScope.information}"/>
                </td>
            </tr>
        </table>
        <table id="judgeTable">
            <tr>
                <td>该项目有以下评委:</td>
            </tr>
        </table>
        <table>
            <tr>
                <td>设置评委的信息</td>
                <td>
                    <input type="text" placeholder="请输入评委的用户名" tabindex="1" name="judgename" id="judgename"/>
                </td>
                <td>
                    <input type="text" placeholder="请输入评委的密码" tabindex="1" name="judgepassword" id="judgepassword"/>
                </td>
            </tr>
            <tr>
                <td>
                    <button id="addJudge" type="button" tabindex="1">
                        添加评委
                    </button>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" value="创建项目" tabindex="1" id="sub_btn"/>
                </td>
            </tr>
        </table>
        <span class="errorMsg">${requestScope.msg}</span>
    </form>
</div>
</body>
</html>
